<?php
namespace app\shop\api;
use app\common\api\BaseApi;
use app\shop\model\OrderRefund;
use app\shop\model\OrdersInfoView;
use app\shop\model\Orders;
use app\shop\model\RefundInfoView;
use app\extend\Page;
/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/20
 * Time: 17:38
 */
class OrderRefundApi extends  BaseApi{
    const ADD="shop/OrderRefundApi/add";

    const QUERY="shop/OrderRefundApi/query";
	
	const QUERY_NO_PAGING="shop/OrderRefundApi/queryNoPaging";

    const SAVE_BY_ID="shop/OrderRefundApi/saveById";

    const SAVE="shop/OrderRefundApi/save";

    const DELETE="shop/OrderRefundApi/delete";

    const GET_INFO ="shop/OrderRefundApi/getInfo";

    const QUERY_WITH_ORDER = "shop/OrderRefundApi/queryWithOrder";

    const CHANGE_SERVICE_STATUS = "shop/OrderRefundApi/changeServerStatus";

    protected  function _init(){
        $this->model=new OrderRefund();
    }

    public function queryWithOrder($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $query = new RefundInfoView();
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }

        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $query -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));

    }

    /**
     * 修改订单售后状态
     */
    public function changeServerStatus($order_code,$cs_status){

        $map = array(
            'order_code'=>$order_code
        );
        //修改订单的售后状态
        $entity = array(
            'cs_status' => $cs_status
        );
        $result = apiCall(OrdersApi::SAVE,array($map,$entity));

        return $result;
    }
}
